﻿using CarAppDomain.Sys.Entities;
using CarAppDto.Sys.AdminDtos;
using CarAppService.Sys.IServices;
using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using D.UtilSqlsugarOrm;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace CarAppAPI
{
    /// <summary>
    /// 管理员模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class AdminController : BaseApiController
    {
        private readonly IAdminService _adminService;
        public AdminController(IAdminService adminService)
        {
            _adminService = adminService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddAdmin"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddAdmin reqAddAdmin)
        {
            // 验证和清理输入数据，防止注入攻击
            var sanitizedComment = System.Web.HttpUtility.HtmlEncode(reqAddAdmin.Descption);

            reqAddAdmin.CreateUser = AdminId;
            return await _adminService.AddAdminAsync(reqAddAdmin);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _adminService.DeleteAdminAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdateAdmin"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateAdmin reqUpdateAdmin)
        {
            reqUpdateAdmin.Id = id;
            reqUpdateAdmin.UpdateUser = AdminId;
            return await _adminService.UpdateAdminAsync(reqUpdateAdmin);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepAdminInfo>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = AdminId;
            }
            return await _adminService.GetAdminAsync(id);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqGueryAdminList"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepAdminInfo>>> List([FromBody] ReqGueryAdminList reqGueryAdminList)
        {
            return await _adminService.QueryAdminListAsync(reqGueryAdminList);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepAdminInfo>>> Pages([FromBody] ReqQueryAdminPage reqQueryAdminPage)
        {
            return await _adminService.QueryAdminPagesAsync(reqQueryAdminPage);
        }
    }
}
